package com.spring.boot.controller;

import com.spring.boot.utils.Chat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

@Controller
public class WebSocketController {

    @Autowired
    SimpMessagingTemplate simpMessagingTemplate;//websocket 的配置类实现了WebSocketMessageBrokerConfigurer接口这个bean就不会报空

//    @PreAuthorize("hasAnyAuthority('ROLE_bigScreen')")//
    @MessageMapping("/mes")
//    @SendTo("/topic")
    public void mes(Principal principal, Chat chat) {
        System.out.println("name:"+principal.getName());
        System.out.println("mes = " + chat);
        simpMessagingTemplate.convertAndSend("/topic",chat);//这里暂时是群发
//        return principal.getName()+":发来致电";
    }
}
